/**
 * 
 */

package com.chengyu.eyc.service;

import java.util.Map;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chengyu.eyc.constant.EycConstants;
import com.chengyu.eyc.exception.GenericException;
import com.chengyu.eyc.properties.SmsProperties;
import com.chengyu.eyc.utils.RedisUtil;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import cn.hutool.core.util.StrUtil;

/**
 * @author kevin
 *
 */
@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsProperties smsProperties;

    /**
     * @see com.chengyu.eyc.service.SmsService#obtainVerifyCode(java.lang.String)
     */
    @Override
    public Boolean obtainVerifyCode(String to) {
        String key = EycConstants.CacheKey.getVerifyCdeKey(to);
        String verifyCde = RedisUtil.exists(to) ? (String)RedisUtil.get(key) : StringUtils.leftPad(String.valueOf(RandomUtils.nextInt(0, 999999)), 6, "0");
        // 生产环境请求地址：app.cloopen.com
        String serverIp = smsProperties.getServer().getHost();
        // 请求端口
        String serverPort = smsProperties.getServer().getPort();
        // 主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = smsProperties.getAccount().getSid();
        String accountToken = smsProperties.getAccount().getToken();
        // 请使用管理控制台中已创建应用的APPID
        String appId = smsProperties.getAppId();
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String templateId = smsProperties.getTemplate().getId();

        String[] datas = {verifyCde, "2分钟"};
        // String subAppend = RandomStringUtils.randomNumeric(4); // 可选 扩展码，四位数字 0~9999
        // String reqId = RandomStringUtils.randomNumeric(32); // 可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        // HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        Map<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas/*, subAppend, reqId*/);
        boolean success = StrUtil.equals("000000", (String)result.get("statusCode"));
        if (success) {
            RedisUtil.set(key, verifyCde, 150);
            return success;
        }
        throw new GenericException((String)result.get("statusMsg"));

        // if (StrUtil.equals("000000", (String)result.get("statusCode"))) {
        // // 正常返回输出data包体信息（map）
        // Map<String, Object> data = (Map<String, Object>)result.get("data");
        // Set<String> keySet = data.keySet();
        // for (String key : keySet) {
        // Object object = data.get(key);
        // System.out.println(key + " = " + object);
        // }
        // } else {
        // // 异常返回输出错误码和错误信息
        // System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        // }

    }

}
